<?php
/*
YHM (Your Hosting Manager) - Copyright 2010 All Rights Reserved. - YHM Group
Released under the Simplified BSD Licence.

Website: http://yhm.co.uk
Licence: http://yhm.co.uk/about/#licence

file: client/tickets.php
author: 
*/

//Init Includes, Sessions and Page Info
require_once("../global.php");
session_start();
checklogin_client();
$username = $_SESSION['user_name'];
$page = "support";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>YHM - Client CP - Tickets</title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
</head>
<body>

<?php include("./header.php"); ?>

<h2>Support Tickets</h2>
<p><a href='./tickets.php'>All Tickets</a> | <a href='./tickets.php?filter=open'>Open Tickets</a> | <a href='./tickets.php?filter=inProgress'>'In Progress' Tickets</a> | <a href='./tickets.php?filter=closed'>Closed Tickets</a><br />&nbsp;</p>
		
		<?php
		//Assign Variables
		$action = $_GET['action'];
		if($action == 'read')
		{
			$ticketId = $_GET['id'];
				
			//Load ticket
			$query = "SELECT `id`,`date`,`title`,`message`,`author`,`gid`,`status` FROM ".TABLE_PREFIX."tickets WHERE `id`='$ticketId'";
			$result = mysql_query($query);
			$row = mysql_fetch_array($result);
			mysql_free_result($result);
				
			$ticketTitle = $row['title'];
				
			echo "<table width=900px border=0>";
			echo "<tr><th colspan=2 align=left>Ticket ID #".$row['id']." &nbsp; &bull; &nbsp; Status: ";
				
			if($row['status'] == 'Open'){
				echo "<span style='color:yellow'>Open</span>";
			} elseif($row['status'] == 'In Progress'){
				echo "<span style='color:orange'>In Progress</span>";
			}elseif($row['status'] == 'Closed'){
				echo "<span style='color:black'>Closed</span>";
			}
				
			echo "</th></tr>";
			echo "<td colspan=2 align=left style='background:#eee;'><h3 style='margin:0;padding:0;font-size:22px;color:black;'>".$row['title']."</h3/></td></tr>";
				
			echo "<tr style='background:#ddd url(./images/support/ticket-bg.png) repeat-x top'><td align=center style='background:#fff;width:120px;'>";
				
			if($row['gid'] == '0'){
				echo "<img src='./images/client.png' style='height:90px;' /><h4>".$row['author']."</h4><span style='color:black'>Client</span>";
			}elseif($row['gid'] == '1'){
				echo "<img src='./images/staff.png' style='height:100px;' /><h4>".$row['author']."</h4><span style='color:black'>Administrator</span>";
			}elseif($row['gid'] == '2'){
				echo "<img src='./images/staff.png' style='height:100px;' /><h4>".$row['author']."</h4><span style='color:black'>Support Staff</span>";
			}
				
			echo "</td><td align=left>".$row['message']."</td></tr>";
			echo "<tr><td align=left>&nbsp;</td><td>".$row['date']."</td></tr>";
				
			// Load ticket replies
			$query = "SELECT `id`,`ticket-id`,`date`,`title`,`message`,`author`,`gid` FROM ".TABLE_PREFIX."ticket-replies WHERE `ticket-id`='$ticketId'";
			$result = mysql_query($query);
			while($row = mysql_fetch_array($result)) {
				echo "<tr style='background:#ddd url(./images/support/ticket-bg.png) repeat-x top'><td align=center style='background:#fff'>";
				
				if($row['gid'] == '0'){
					echo "<img src='./images/client.png' style='height:90px;' /><h4>".$row['author']."</h4><span style='color:black'>Client</span>";
				}elseif($row['gid'] == '1'){
					echo "<img src='./images/staff.png' style='height:100px;' /><h4>".$row['author']."</h4><span style='color:black'>Administrator</span>";
				}elseif($row['gid'] == '2'){
					echo "<img src='./images/staff.png' style='height:100px;' /><h4>".$row['author']."</h4><span style='color:black'>Support Staff</span>";
				}
					
				echo "</td><td align=left>".$row['message']."</td></tr>";
				echo "<tr><td align=left>&nbsp;</td><td>".$row['date']."</td></tr>";
			}
				
			echo "<form action='".$_SERVER['PHP_SELF']."?page=tickets&action=update&id=".$ticketId."' method='POST'>";
					
			echo "<tr style='background:#ddd url(./images/support/ticket-reply-bg.png) repeat-x top'>
			<td align=center><img src='./images/staff.png' style='height:100px;' /><h4>".$fullName."</h4><span style='color:white'>Administrator</span></td>
				
			<td><b style='color:white;'>Your Response:</b><br /><textarea name='message' style='height:120px;width:730px'></textarea><br />
			<span style='float:right;margin-right:20px;'>
				
			<input type='hidden' value='$ticketTitle' name='title'>
			
				
			<label style='color:white;'><input type='checkbox' name='closeTicket' value='yes'> Close this ticket</label> &nbsp; <input type='submit' value='Post Reply'></td></tr>";
				
			echo "</form>";
				
			echo "</table>";
			
			}elseif($action == 'update'){
				// Post the data from the form
				$ticketId = $_GET['id'];
				$date = date('Y-m-d H:i:s');
				$title = $_POST['title'];
				$message = $_POST['message'];
				$message = nl2br($message);
				$author = $fullName;
				$gid = $groupId;
				
				
				
			
				// Send the new reply to the database
				$query = "INSERT INTO ".TABLE_PREFIX."ticket-replies (`ticket-id`,`date`,`title`,`message`,`author`,`gid`) VALUES ('$ticketId','$date','$title','$message','$author','$gid')";
				$result = mysql_query($query);
				
				// Check if the 'close ticket' checkbox is ticked or not. If not then skip this section
				$closeTicket = $_POST['closeTicket'];
				if($closeTicket == 'yes'){
					// Run query to close the ticket
					$query = "UPDATE `tickets` SET status='Closed' WHERE `id`='$ticketId'";
					$result = mysql_query($query) or die('MySQL Error');
				}else{
					// Update the status in the db to be 'In Progress'
					$query = "UPDATE `tickets` SET status='In Progress' WHERE `id`='$ticketId'";
					$result = mysql_query($query) or die('MySQL Error');
				}
				
				echo "<p>The ticket has been updated successfully.<br />
				<a href='".$_SERVER['PHP_SELF']."?page=tickets&action=read&id=".$ticketId."'>Back to ticket</a></p>";
				
			}elseif(!$action) {
		?>
		
		<p>
		<table width='900px' border=1>
			<tr>
				<th align=left colspan=5>Support Tickets</th>
			</tr>
			
			<tr>
				<td width=80px align=center style='background:#eee'><b>Status</b></td>
				<td width=60 align=center style='background:#eee'><b>#ID</b></td>
				<td align=center style='background:#eee'><b>Title</b></td>
				<td width=200px align=center style='background:#eee'><b>Author</b></td>
				<td width=180px align=center style='background:#eee'><b>Date/Time</b></td>
			</tr>
			
			
			
			<?php
			$filter = $_GET['filter'];
			if(!$filter){
				//Show all tickets
				$query = "SELECT `id`,`date`,`title`,`message`,`author`,`status` FROM ".TABLE_PREFIX."tickets ORDER BY `date` DESC";
				$result = mysql_query($query);
				while($row = mysql_fetch_array($result)){
					if($row['status'] == 'Open'){
						echo "<tr>
							<td width=22px style='background:#FFF6BF' align=center><b>".$row['status']."</b></td>";
						echo "<td style='background:#FFF6BF'><b style='color:black;'>#".$row['id']."</b></td>";
						echo "<td style='background:#FFF6BF'><a href='".$_SERVER['PHP_SELF']."?page=tickets&action=read&id=".$row['id']."'><b style='color:black;'>".$row['title']."</b></td>";
						echo "<td width=150px style='background:#FFF6BF'><b>".$row['author']."</b></td>";
						echo "<td width=140px align=center style='background:#FFF6BF'><b>".$row['date']."</b></td>
							</tr>";
					} elseif($row['status'] == 'In Progress') {
						echo "<tr>
							<td width=22px align=center>".$row['status']."</td>";
						echo "<td>#".$row['id']."</td>";
						echo "<td><a href='".$_SERVER['PHP_SELF']."?page=tickets&action=read&id=".$row['id']."'><span style='color:black;'>".$row['title']."</span></td>";
						echo "<td width=150px>".$row['author']."</td>";
						echo "<td width=140px align=center>".$row['date']."</td>
							</tr>";
					} elseif($row['status'] == 'Closed') {
						echo "<tr>
							<td width=22px align=center><span style='color:grey;'>".$row['status']."</span></td>";
						echo "<td><span style='color:grey;'>#".$row['id']."</span></td>";
						echo "<td><a href='".$_SERVER['PHP_SELF']."?page=tickets&action=read&id=".$row['id']."'><span style='color:grey;'>".$row['title']."</span></a></td>";
						echo "<td width=150px><span style='color:grey;'>".$row['author']."</span></td>";
						echo "<td width=140px align=center><span style='color:grey;'>".$row['date']."</span></td>
							</tr>";
					}
				}
				mysql_free_result($result);
			}elseif($filter == 'open'){
				//Show open tickets
				$query = "SELECT `id`,`date`,`title`,`message`,`author`,`status` FROM ".TABLE_PREFIX."tickets WHERE `status`='Open' ORDER BY `date` DESC";
				$result = mysql_query($query);
				while($row = mysql_fetch_array($result)){
					echo "<tr>
						<td width=22px style='background:#FFF6BF' align=center><b>".$row['status']."</b></td>";
					echo "<td style='background:#FFF6BF'><b style='color:black;'>#".$row['id']."</b></td>";
					echo "<td style='background:#FFF6BF'><a href='".$_SERVER['PHP_SELF']."?page=tickets&action=read&id=".$row['id']."'><b style='color:black;'>".$row['title']."</b></td>";
					echo "<td width=150px style='background:#FFF6BF'><b>".$row['author']."</b></td>";
					echo "<td width=140px align=center style='background:#FFF6BF'><b>".$row['date']."</b></td>
						</tr>";
				}
			}elseif($filter == 'inProgress'){
				//Show 'In Progress' tickets
				$query = "SELECT `id`,`date`,`title`,`message`,`author`,`status` FROM ".TABLE_PREFIX."tickets WHERE `status`='In Progress' ORDER BY `date` DESC";
				$result = mysql_query($query);
				while($row = mysql_fetch_array($result)){
					echo "<tr>
						<td width=22px align=center>".$row['status']."</td>";
					echo "<td>#".$row['id']."</td>";
					echo "<td><a href='".$_SERVER['PHP_SELF']."?page=tickets&action=read&id=".$row['id']."'><span style='color:black;'>".$row['title']."</span></td>";
					echo "<td width=150px>".$row['author']."</td>";
					echo "<td width=140px align=center>".$row['date']."</td>
						</tr>";
				}
			}elseif($filter == 'closed'){
				//Show closed tickets
				$query = "SELECT `id`,`date`,`title`,`message`,`author`,`status` FROM ".TABLE_PREFIX."tickets WHERE `status`='Closed' ORDER BY `date` DESC";
				$result = mysql_query($query);
				while($row = mysql_fetch_array($result)){
					echo "<tr>
							<td width=22px align=center><span style='color:grey;'>".$row['status']."</span></td>";
					echo "<td><span style='color:grey;'>#".$row['id']."</span></td>";
					echo "<td><a href='".$_SERVER['PHP_SELF']."?page=tickets&action=read&id=".$row['id']."'><span style='color:grey;'>".$row['title']."</span></a></td>";
					echo "<td width=150px><span style='color:grey;'>".$row['author']."</span></td>";
					echo "<td width=140px align=center><span style='color:grey;'>".$row['date']."</span></td>
							</tr>";
				}
			}
			?>
		</table>
		</p>
		<?php
			} // End the if($action ) tag
		?>

<?php include ("../footer.php"); ?>
</div>
</body>
</html>